﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listStaticSegmentDelMembersOutput : Api_BaseOutput
   {
      public listStaticSegmentDelMembersResults result { get; set; }
      public listStaticSegmentDelMembersParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listStaticSegmentDelMembersOutput( listStaticSegmentDelMembersInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new listStaticSegmentDelMembersResults();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Segment Members; deleted: {0}, errors: {0}", this.result.success, this.result.errors.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_listStaticSegmentDelMembers rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int it;
            listStaticSegmentDelMembersResults results = new listStaticSegmentDelMembersResults();

            int.TryParse( xml.Element( "success" ).Value, out it );
            results.success = it;
            if ( xml.Element( "errors" ).HasElements )
            {
               IEnumerable<XElement> errors = xml.Element( "errors" ).Elements( "struct" );
               foreach ( var elem in errors )
               {
                  listStaticSegmentDelMembersError am_error = new listStaticSegmentDelMembersError();
                  am_error.email = elem.Element( "email" ).Value;
                  int.TryParse( elem.Element( "code" ).Value, out it );
                  am_error.code = it;
                  am_error.msg = elem.Element( "msg" ).Value;
                  //
                  results.errors.Add( am_error );
               }
            }
            this.result = results;

         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listStaticSegmentDelMembers jsonResult = jss.Deserialize<x_listStaticSegmentDelMembers>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }

      }

      // ---------------------------------------------------------
      private listStaticSegmentDelMembersResults FormatResults( x_listStaticSegmentDelMembers results_array )
      {
         listStaticSegmentDelMembersResults results = new listStaticSegmentDelMembersResults();
         //
         results.success = results_array.success;
         foreach ( x_listStaticSegmentDelMembersError error in results_array.errors )
         {
            results.errors.Add( new listStaticSegmentDelMembersError
            {
               email = error.email,
               code = error.code,
               msg = error.msg
            } );
         }
         //
         return results;
      }

      #endregion
   }
}
